package view;

import controller.Client;
import model.User_Friend;
import org.junit.Test;
import service.AddFriends;
import service.SelectFriends;
import service.SendMessage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class FriendList extends JFrame implements ActionListener {
    JLabel jb1, jb2, jb3;
    Integer sendId;
    Socket mysocket;
    List<JLabel> friend;
    JButton jb;
    JTextField jtf;


    public FriendList(Integer id, final Socket socket) {
        //用户id 和 socket
        sendId = id;
        mysocket = socket;
        //好友界面
        jb1 = new JLabel(new ImageIcon(getClass().getResource("/backGRUOND.jpg")));
        jb1.setBounds(0, 0, 360, 600);
        add(jb1);

        jb2 = new JLabel("尊敬的用户:" + id);
        jb2.setBounds(10, 10, 200, 30);
        jb2.setFont(new Font("宋体", Font.BOLD, 20));
        jb1.add(jb2);

        jb3 = new JLabel("好友列表");
        jb3.setBounds(100, 22, 260, 80);
        jb3.setFont(new Font("宋体", Font.BOLD, 30));
        jb1.add(jb3);

        //查询该用户所有好友的id
        List<Integer> friends = new SelectFriends().selectFriends(id);
        int height = 110;

        friend = new ArrayList<JLabel>();

        //循环显示好友头像和id
        for (Integer user_id : friends) {
            JLabel jl1 = new JLabel(new ImageIcon(getClass().getResource("/ali.jpg")));
            jl1.setBounds(30, height, 48, 52);
            jb1.add(jl1);

            JLabel jl2 = new JLabel(user_id.toString());
            jl2.setBounds(130, height, 200, 52);
            jl2.setFont(new Font("宋体", Font.BOLD, 20));
            jb1.add(jl2);

            height += 60;

            //将好友放入数组中为下面循环用
            friend.add(jl2);
        }
        //为每一个好友设置监听
        for (final JLabel jl : friend) {
            jl.addMouseListener(new MouseListener() {
                @Override
                /*
                j1.getText()就是用户要点击聊天对象的id
                然后双方互发消息
                每次点击一个就启动一个线程
                 */

                public void mouseClicked(MouseEvent e) {
                    Integer getid = Integer.parseInt(jl.getText());
                    SendMessage sendMessage = new SendMessage(sendId, getid, mysocket);
                    Thread t = new Thread(sendMessage);
                    t.start();
                }

                @Override
                public void mousePressed(MouseEvent e) {

                }

                @Override
                public void mouseReleased(MouseEvent e) {

                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    jl.setFont(new Font("宋体", Font.BOLD, 35));
                    jl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    jl.setFont(new Font("宋体", Font.BOLD, 25));
                }
            });
        }

        //add friends
        jb = new JButton("添加好友");
        jb.setBounds(15, 500, 100, 30);
        jb.setFont(new Font("宋体", Font.BOLD, 15));
        jb.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jb.addActionListener(this);
        jb1.add(jb);

        jtf = new JTextField();
        jtf.setBounds(135, 500, 165, 30);
        jb1.add(jtf);


        setTitle("qq");
        setLayout(null);
        setSize(360, 600);
        setLocation(1100, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        setResizable(false);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        try {
            if (e.getSource() == jb) {
                String friend_id = jtf.getText();
                User_Friend user_friend = new User_Friend(sendId, Integer.parseInt(friend_id));
                int flag = new AddFriends().addFriend(user_friend);
                if (flag == 1) {
                    JOptionPane.showMessageDialog(null, "添加好友成功", "success", JOptionPane.PLAIN_MESSAGE);
                } else if (flag == 2) {
                    JOptionPane.showMessageDialog(null, "该好友已经存在", "error", JOptionPane.ERROR_MESSAGE);
                } else if (flag == 3) {
                    JOptionPane.showMessageDialog(null, "没有该用户", "warning", JOptionPane.ERROR_MESSAGE);
                } else if (flag == 0) {
                    JOptionPane.showMessageDialog(null, "Don't add yourself", "warning", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "系统繁忙,请稍后再试", "warning", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (NumberFormatException e1) {
            System.out.println("好友的ID不能为空");
        }
    }
}
